/*
 * @Author: hongbin
 * @Date: 2022-09-04 22:01:04
 * @LastEditors: hongbin
 * @LastEditTime: 2022-09-27 16:22:35
 * @Description:加载器
 */
import * as THREE from "three";
import { GLTF, GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader.js";

export const manager = new THREE.LoadingManager();
export const dracoLoader = new DRACOLoader(manager);
dracoLoader.setDecoderConfig({ type: "js" });
// dracoLoader.setDecoderPath("https://api.hongbin.xyz:3002/kmyc/");
// const dracoLoader = new DRACOLoader(manager);
dracoLoader.setDecoderPath("draco/");
// dracoLoader.setDecoderPath("api/draco/");

const gltfLoader = new GLTFLoader(manager);
gltfLoader.setDRACOLoader(dracoLoader);

export const loadGltf = (url: string): Promise<GLTF> => {
    return new Promise((res, rej) => {
        gltfLoader.load(
            url,
            (gltf: GLTF) => {
                // console.log("gltf", gltf);
                res(gltf);
            },
            () => {},
            (err) => {
                // console.log("err", err);
                rej(err);
            }
        );
    });
};

manager.onStart = function (url, itemsLoaded, itemsTotal) {
    // console.log("开始加载文件: " + url + ".\nLoaded " + itemsLoaded + " of " + itemsTotal + " files.");
};

manager.onLoad = function () {
    console.log("加载完成!");
    // loadInfoControls.addInfo("主线程加载完成", 100);
    // window.loadFinish();
    window.render();
};

manager.onProgress = function (url, itemsLoaded, itemsTotal) {
    window.render();
    // console.log("load：", url);

    // console.log("加载中文件: " + url + ".\nLoaded " + itemsLoaded + " of " + itemsTotal + " files.");
    // window.updateProgress(Math.floor((itemsLoaded / itemsTotal) * 100));
};

manager.onError = function (url) {
    console.log("加载出错：" + url);
};

export const textureLoader = new THREE.TextureLoader(manager);
